// Copyright 2012 Google Inc. All Rights Reserved.

package com.google.appengine.syncserver.shared;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import java.io.IOException;

/**
 * Json utils for converting objects to string and back.
 */
public class JsonUtils {
  public static String objectToString(Object object)
      throws JsonGenerationException, JsonMappingException, IOException {
    ObjectMapper mapper = new ObjectMapper();
    return mapper.writeValueAsString(object);
  }

  public static Object stringToObject(String str, Class<?> clazz)
      throws JsonParseException, JsonMappingException, IOException {
    ObjectMapper mapper = new ObjectMapper();
    // TODO(chintanraoh): return object of type clazz.
    return mapper.readValue(str, clazz);
  }
}
